FROM alpine:latest

ENV MYSQL_DB_HOST localhost
ENV MYSQL_DB_ROOT root
ENV MYSQL_DB_ROOT_PASS 1234
ENV MYSQL_DB_NAME testlink
ENV MYSQL_DB_USER testlink
ENV MYSQL_DB_PASS testlink123

RUN echo -e "http://mirrors.ustc.edu.cn/alpine/v3.3/main/\nhttp://dl-cdn.alpinelinux.org/alpine/v3.3/community" > /etc/apk/repositories \
&& apk update \
&& apk add php php-fpm php-pdo php-pdo_mysql php-pdo_sqlite php-bz2 php-xmlrpc php-mcrypt php-memcache php-gd php-xml php-xcache php-xml php-mysql php-sqlite3 php-cli php-zip php-dom php-curl php-mysqli \
&& sed -i "s/127.0.0.1:9000/0.0.0.0:9000/" /etc/php/php-fpm.conf \
&& echo "max_execution_time=3000" >> /etc/php/php.ini \
&& rm -rf /var/cache/apk/*

RUN apk add --no-cache apache2 php-apache2 apache2-ldap apache2-icons apache2-error apache2-utils \
&& echo "ServerName 0.0.0.0:80" >> /etc/apache2/httpd.conf \
&& mkdir -p /run/apache2 \
&& echo -e "<?php\nphpinfo();" > /var/www/localhost/htdocs/index.php \
&& rm -rf /var/www/localhost/htdocs/index.html

RUN cd /var/www/localhost/htdocs && rm -rf * \
&& wget -q http://git.oschina.net/wapznw/testlink/raw/master/testlink-1.9.14.tar.gz -O testlink-1.9.14.tar.gz \
&& tar zxvf testlink-1.9.14.tar.gz \
&& mv testlink-1.9.14/* ./ \
&& rm -rf testlink-1.9.14 \
&& rm -rf testlink-1.9.14.tar.gz \
&& mkdir -p /var/testlink/logs \
&& mkdir -p /var/testlink/upload_area \
&& chmod -R 777 /var/www/localhost/htdocs /var/testlink/logs /var/testlink/upload_area \
&& sed -i 's/value="localhost"/value="<?php echo getenv("MYSQL_DB_HOST"); ?>"/' /var/www/localhost/htdocs/install/installDbInput.php \
&& sed -i 's/value="testlink"/value="<?php echo getenv("MYSQL_DB_NAME"); ?>"/' /var/www/localhost/htdocs/install/installDbInput.php \
&& sed -i 's/id="databaseloginname"/id="databaseloginname" value="<?php echo getenv("MYSQL_DB_ROOT"); ?>"/' /var/www/localhost/htdocs/install/installDbInput.php \
&& sed -i 's/id="databaseloginpassword"/id="databaseloginpassword" value="<?php echo getenv("MYSQL_DB_ROOT_PASS"); ?>"/' /var/www/localhost/htdocs/install/installDbInput.php \
&& sed -i 's/id="tl_loginname"/id="tl_loginname" value="<?php echo getenv("MYSQL_DB_USER"); ?>"/' /var/www/localhost/htdocs/install/installDbInput.php \
&& sed -i 's/id="tl_loginpassword"/id="tl_loginpassword" value="<?php echo getenv("MYSQL_DB_PASS"); ?>"/' /var/www/localhost/htdocs/install/installDbInput.php 

EXPOSE 80

CMD ["httpd", "-D", "FOREGROUND"]

# docker build -t maosui/alpine-testlink:latest .

